package com.sunny.ynhz.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：huhuan on 2016-11-3 19:15
 * 邮箱：1019044763.com
 */
public class TimeFormatUtils {
    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }

    public static String getWeekOfDay(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return getWeekOfDate(strtodate);
    }

    public static String getTime() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return dateFormater.format(date);
    }

    public static String getStartTime() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = dateFormater.format(date);
        return format + " 00:00";
    }


    public static String getTime(String SqlServerTime, String fomate) {

        return TimeFormatUtils.DateToStr(TimeFormatUtils.StrToDate(SqlServerTime), fomate);
    }



    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//字符串格式
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String DateToStr(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }


    /**
     * date转到字符串格式
     *
     * @param date       date格式
     * @param timeFormat 字符串格式
     * @return 时间字符串
     */
    public static String DateToStr(Date date, String timeFormat) {

        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        String str = format.format(date);
        return str;
    }


    public static boolean isSameDay(Date day1, Date day2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ds1 = sdf.format(day1);
        String ds2 = sdf.format(day2);
        if (ds1.equals(ds2)) {
            return true;
        } else {
            return false;
        }
    }



    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(long seconds,String format) {
        SimpleDateFormat dateformat = new SimpleDateFormat(format);
        String dateStr = dateformat.format(seconds);
        return dateStr;
    }





}
